#define _CRT_SECURE_NO_WARNINGS 1
/**
*      author: Y_Yao
*      created: 2024-10-17 18:34:32
**/

//#include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;
using LL = long long;
const LL LLINF = 1e18;
const int INF = 0x3f3f3f3f;
const int N = 6e3 + 7;
int r[N];
vector<vector<int>> tr(N);
bool fa[N];
int f[N][2];

void bfs(int root)
{
    f[root][1] = r[root];
    for (auto& e : tr[root])
    {
        bfs(e);
        f[root][0] += max(f[e][0], f[e][1]);
        f[root][1] += f[e][0];
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n = 0;
    cin >> n;
    for (int i = 1;i <= n;i++)
    {
        cin >> r[i];
    }

    for (int i = 0;i < n - 1;i++)
    {
        int l, k;
        cin >> l >> k;
        tr[k].push_back(l);
        fa[l] = true;
    }
    int root = 1;
    while (fa[root]) root++;
    bfs(root);

    cout << max(f[root][0], f[root][1]) << endl;
    return 0;
}